package com.clps.digital.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.clps.digital.common.BaseResponse;
import com.clps.digital.common.ErrorCode;
import com.clps.digital.common.ResultUtils;
import com.clps.digital.model.dto.user.UserLoginRequest;
import com.clps.digital.model.dto.user.UserRegisterRequest;
import com.clps.digital.model.dto.user.UserUpdateRequest;
import com.clps.digital.model.entity.User;
import com.clps.digital.model.vo.UserLoginVo;
import com.clps.digital.service.UserService;
import com.clps.digital.util.TokenCache;
import com.clps.digital.util.UserContext;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * @author: Chase
 * @date: 2024/8/29 21:31
 * @Version: 1.0
 * @description:
 */

public class UserController {

    private UserService userService;

    /**
     * 用户登录
     * @param userLoginRequest
     * @return
     */
    public BaseResponse<UserLoginVo> userLogin(UserLoginRequest userLoginRequest) {
        if (ObjectUtil.isEmpty(userLoginRequest) || ObjectUtil.isEmpty(userLoginRequest.getUserAccount()) ||
                ObjectUtil.isEmpty(userLoginRequest.getUserPassword())) {
            return ResultUtils.error(ErrorCode.LOGIN_ERROR);
        }
        UserLoginVo userLoginVo = userService.login(userLoginRequest);
        if (ObjectUtil.isEmpty(userLoginVo)) {
            return ResultUtils.error(ErrorCode.LOGIN_ERROR);
        }
        return ResultUtils.success(userLoginVo);
    }

    /**
     * 用户登出
     * @param request
     * @return
     */
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
        String token = request.getHeader("token");
        TokenCache.remove(token);
        return ResultUtils.success(true);
    }

    /**
     * 注册用户
     * @param userRegisterRequest
     * @return
     */
    public BaseResponse<Boolean> registerUser(UserRegisterRequest userRegisterRequest) {
        if (!userRegisterRequest.getUserPassword().equals(userRegisterRequest.getCheckPassword())) {
            return ResultUtils.error(ErrorCode.OPERATION_ERROR, "用户名或密码为空");
        }
        User user = new User();
        BeanUtil.copyProperties(userRegisterRequest, user);
        Long userId = userService.insertUser(user);
        if (ObjectUtil.isEmpty(userId)) {
            return ResultUtils.error(ErrorCode.REGISTER_ERROR);
        }
        return ResultUtils.success(true);
    }

    /**
     * 获取当前登录的用户信息
     * @return
     */
    public BaseResponse<User> getCurrentUser() {
        Long userId = UserContext.getUser();
        User user = userService.getUser(userId);
        if (ObjectUtil.isEmpty(user)) {
            return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(user);
    }

    /**
     * 更新用户信息
     * @param userUpdateRequest
     * @return
     */
    public BaseResponse<Boolean> updateCurrentUser(UserUpdateRequest userUpdateRequest) {
        // TODO
        User user = new User();
        BeanUtil.copyProperties(userUpdateRequest, user);
        Boolean flag = userService.updateUser(user);
        if (flag) {
            return ResultUtils.success(true);
        }
        return ResultUtils.error(ErrorCode.OPERATION_ERROR);
    }

    /*
    * @Author 小炎子
    * @Description 用户充值余额
    * @Date 上午10:25 2024/9/5
    */
    public BaseResponse<String> addBalance(Integer money) {
        userService.addBalance(money);
        return ResultUtils.success("充值成功");
    }
}
